package com.utils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.db1.Db_Common1;
import com.zxy.db_Self.Db_Common_Self;
import com.zxy.model.fine_eova_role;
import com.zxy.model.fine_eova_user;

public class TOKEN_USER implements com.zxy.common.ParamName_InterFace
{
	public void init_start()
	{
		List<fine_eova_role> fine_eova_role_objs = get_fine_eova_role();
		List<fine_eova_user> fine_eova_user_objs = get_fine_eova_user();
		for(fine_eova_user feu: fine_eova_user_objs)
		{
			// sys_user(feu);
			// eova中不存在用户信息
			if(feu.login_id.equals(""))
			{
				fine_eova_role fn = new fine_eova_role();
				for(fine_eova_role fer: fine_eova_role_objs)
				{
					if(fer.userId.equals(feu.user_id))
					{
						fn = fer;
						break;
					}
				}

				ins_eova_user(feu,fn);
				feu.eova_user_id = get_eova_user_id(feu);
				upd_fine_eova_user(feu);
			}else
			{
				fine_eova_role fn = new fine_eova_role();
				for(fine_eova_role fer: fine_eova_role_objs)
				{
					if(fer.userId.equals(feu.user_id))
					{
						fn = fer;
						break;
					}
				}

				upd_eova_user(feu,fn);
				feu.eova_user_id = get_eova_user_id(feu);
				upd_fine_eova_user(feu);
			}
		}
	}

	private String get_sys_user_MAIN_ID(fine_eova_user feu)
	{
		Db_Common1 dbc1 = new Db_Common1();
		String MAIN_ID = "";
		StringBuilder strSql = new StringBuilder();
		strSql.append("select * from sys_user where login_name = '"
				+ feu.userName + "'");
		ResultSet rs = dbc1.Common_Sql(strSql);
		try
		{
			while(rs.next())
			{
				MAIN_ID = rs.getString("MAIN_ID");
				break;
			}
		}
		catch(SQLException e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
		finally
		{
			if(rs != null)
			{
				try
				{
					rs.getStatement().close();
					rs.close();
				}
				catch(SQLException e)
				{
				}
			}
		}
		return MAIN_ID;
	}

	private void sys_user(fine_eova_user feu)
	{
		String MAIN_ID = get_sys_user_MAIN_ID(feu);
		StringBuilder strSql = new StringBuilder();
		if(MAIN_ID.equals(""))
		{
			strSql.delete(0,strSql.length());
			strSql.append("insert into sys_user(user_id,login_name,depart_id,user_name,password) "
					+ " values('0','" + feu.userName + "','1','" + feu.realName
					+ "','" + feu.password + "')");
		}else
		{
			strSql.delete(0,strSql.length());
			strSql.append("update sys_user set user_name='" + feu.realName
					+ "',password='" + feu.password + "' where MAIN_ID = '"
					+ MAIN_ID + "'");
		}
		Db_Common1 dbc1 = new Db_Common1();
		dbc1.CommonExec_Sql(strSql);
	}

	private void upd_fine_eova_user(fine_eova_user feu)
	{
		Db_Common_Self dbc_self = new Db_Common_Self();
		StringBuilder strSql = new StringBuilder("update fine_user_upd set is_upd='2',eova_user_id='"
				+ feu.eova_user_id + "' where user_id='" + feu.user_id + "' ");
		dbc_self.CommonExec_Sql(strSql);
	}

	private void upd_eova_user(fine_eova_user feu, fine_eova_role fer)
	{
		Db_Common_Self dbc_self = new Db_Common_Self();
		StringBuilder strSql = new StringBuilder("update eova_user set login_id='"
				+ feu.userName + "',login_pwd='" + feu.password + "',nickname='"
				+ feu.realName + "',rid='" + fer.eova_role_id + "' where id='"
				+ feu.id + "'");
		dbc_self.CommonExec_Sql(strSql);
	}

	private void ins_eova_user(fine_eova_user feu, fine_eova_role fer)
	{
		Db_Common_Self dbc_self = new Db_Common_Self();
		StringBuilder strSql = new StringBuilder("insert into eova_user(login_id,login_pwd,nickname,rid) values('"
				+ feu.userName + "','" + feu.password + "','" + feu.realName
				+ "','" + fer.eova_role_id + "')");
		dbc_self.CommonExec_Sql(strSql);
	}

	private String get_eova_user_id(fine_eova_user feu)
	{
		String strID = "";
		Db_Common_Self dbc_self = new Db_Common_Self();
		StringBuilder strSql = new StringBuilder();
		strSql.append("select * from eova_user where login_id = '"
				+ feu.userName + "'");
		ResultSet rs = dbc_self.Common_Sql(strSql);
		try
		{
			while(rs.next())
			{
				strID = rs.getString("id");
				break;
			}
		}
		catch(SQLException e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
		finally
		{
			if(rs != null)
			{
				try
				{
					rs.getStatement().close();
					rs.close();
				}
				catch(SQLException e)
				{
				}
			}
		}
		return strID;
	}

	// fine_eova_role
	private List<fine_eova_role> get_fine_eova_role()
	{
		Db_Common_Self dbc_self = new Db_Common_Self();
		List<fine_eova_role> td = new ArrayList<fine_eova_role>();
		StringBuilder strSql = new StringBuilder();
		strSql.append("select a.userId,a.roleId,b.name,c.eova_role_id from fine_user_role_middle a "
				+ " left join fine_custom_role b on a.roleId = b.id "
				+ " left join fine_eova_role c on a.roleid = c.fine_role_id  where b.name != '' and b.name is not null order by a.userId");
		ResultSet rs = dbc_self.Common_Sql(strSql);
		try
		{
			while(rs.next())
			{
				fine_eova_role tdc = new fine_eova_role();
				for(Field field: tdc.getClass().getDeclaredFields())
				{
					field.setAccessible(true);
					if(field.getType().toString().equals("int"))
						field.set(tdc,rs.getInt(field.getName()));
					else if(field.getType().toString().equals("class java.lang.String"))
						field.set(tdc,Com_Fun.NoNull(rs.getString(field.getName())));
				}
				td.add(tdc);
			}
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
		finally
		{
			if(rs != null)
			{
				try
				{
					rs.getStatement().close();
					rs.close();
				}
				catch(SQLException e)
				{
				}
			}
		}
		return td;
	}

	private List<fine_eova_user> get_fine_eova_user()
	{
		Db_Common_Self dbc_self = new Db_Common_Self();
		List<fine_eova_user> td = new ArrayList<fine_eova_user>();
		StringBuilder strSql = new StringBuilder();
		strSql.append("SELECT a.user_id,a.is_upd,a.eova_user_id,b.password,b.realName,b.userName,b.enable,c.login_id,c.id,c.nickname,c.rid,c.login_pwd "
				+ " from fine_user_upd a left join fine_user b on a.user_id = b.id "
				+ " left join eova_user c on b.userName = c.login_id where is_upd = 0 order by a.user_id;");
		ResultSet rs = dbc_self.Common_Sql(strSql);
		try
		{
			while(rs.next())
			{
				fine_eova_user tdc = new fine_eova_user();
				for(Field field: tdc.getClass().getDeclaredFields())
				{
					field.setAccessible(true);
					if(field.getType().toString().equals("int"))
						field.set(tdc,rs.getInt(field.getName()));
					else if(field.getType().toString().equals("class java.lang.String"))
						field.set(tdc,Com_Fun.NoNull(rs.getString(field.getName())));
				}
				td.add(tdc);
			}
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
		finally
		{
			if(rs != null)
			{
				try
				{
					rs.getStatement().close();
					rs.close();
				}
				catch(SQLException e)
				{
				}
			}
		}
		return td;
	}
}